package com.wjzjq.hiring.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.wjzjq.hiring.company.entity.CompanyEntity;

public class EmailUtils {
	// serverEmail
	private static final String FROM = "15923233578@163.com";
	private static final String CHECK_CODE = "checkCode";

	/**
	 * 生成重设密码的链接
	 */
	public static String generateResetPwdLink(CompanyEntity company) {
		return "http://localhost:8080/hiring/CompanyServlet?command=toResetPassword&code=" + company.getCode() + "&"
				+ CHECK_CODE + "=" + generateCheckcode(company);
	}

	public static String generateCheckcode(CompanyEntity company) {
		String userName = company.getCode();
		// 用电话代替随机码
		String randomCode = company.getPhone();
		return md5(userName + ":" + randomCode);
	}

	private static String md5(String string) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("md5");
			md.update(string.getBytes());
			byte[] md5Bytes = md.digest();
			return bytes2Hex(md5Bytes);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		return null;
	}

	private static String bytes2Hex(byte[] byteArray) {
		StringBuffer strBuf = new StringBuffer();
		for (int i = 0; i < byteArray.length; i++) {
			if (byteArray[i] >= 0 && byteArray[i] < 16) {
				strBuf.append("0");
			}
			strBuf.append(Integer.toHexString(byteArray[i] & 0xFF));
		}
		return strBuf.toString();
	}

	/**
	 * 发送重设密码链接的邮件
	 */
	public static void sendResetPasswordEmail(CompanyEntity company) {
		Session session = getSession();
		MimeMessage message = new MimeMessage(session);
		try {
			message.setSubject("找回您的帐户与密码");
			message.setSentDate(new Date());
			message.setFrom(new InternetAddress(FROM));
			message.setRecipient(RecipientType.TO, new InternetAddress(company.getEmail()));
			message.setContent("要使用新的密码, 请使用以下链接启用密码:<br/><a href='" + generateResetPwdLink(company) + "'>点击重新设置密码</a>",
					"text/html;charset=utf-8");
			// 发送邮件
			Transport.send(message);
			// System.out.println(request.getRemoteAddr());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Session getSession() {
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.smtp.host", "smtp.163.com");
		props.setProperty("mail.smtp.port", "25");
		props.setProperty("mail.smtp.auth", "true");
		Session session = Session.getInstance(props, new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {

				return new PasswordAuthentication(FROM, "zheng123222");
			}

		});
		return session;
	}
}

